package proxy;

import com.alibaba.fastjson.JSONObject;
import com.ctrip.framework.apollo.ConfigService;
import domain.BaseResponse;
import enums.BaseResponseEnum;
import utils.ApacheHttpUitls;
import utils.ApolloUtils;
import utils.CompressUtil;
import utils.OKHttpUtils;
import lombok.extern.slf4j.Slf4j;
import okhttp3.RequestBody;
import org.apache.commons.lang.StringUtils;

@Slf4j
public class FateProxy {
    private static String baseUrl;

    private static String getUrl() {
        if (StringUtils.isBlank(baseUrl)) {
            baseUrl = ConfigService.getAppConfig().getProperty(ApolloUtils.FATEURL, ApolloUtils.FATEURL_DEFAULT);
        }
        ConfigService.getAppConfig().addChangeListener(changeEvent -> {
            if (changeEvent.isChanged(ApolloUtils.FATEURL)) {
                baseUrl = ConfigService.getAppConfig().getProperty(ApolloUtils.FATEURL, ApolloUtils.FATEURL_DEFAULT);
            }
        });
        return baseUrl;
    }

    /**
     * 请求Fate平台数据上传接口
     * @param requestString 请求参数
     * @return BaseResponseEnum
     */
    public static BaseResponse dataUpload(RequestBody requestString) {

        String url = getUrl() +"/data/upload";
        String responseString;
        try {
            responseString = OKHttpUtils.getFormPostResponse(url, requestString);
            if(StringUtils.isBlank(responseString)){
                return BaseResponse.error();
            }
            JSONObject fateResponse = JSONObject.parseObject(responseString);
            if (FateResCode.SUCCESS.equals(fateResponse.getString("retcode"))) {
                return BaseResponse.success(fateResponse);
            } else {
                log.error("fateResCode is not 0, res is {}", responseString);
                return BaseResponse.error();
            }
        } catch (Exception e) {
            log.error("get response error : ", e);
            return BaseResponse.error();
        }
    }

    /**
     * 请求Fate平台数据绑定接口
     * @param requestString 请求参数
     * @return BaseResponseEnum
     */
    public static BaseResponseEnum tableBind(String requestString) {

        String url = getUrl() +"/table/bind";
        String responseString;
        try {
            responseString = OKHttpUtils.getPostResponse(url, requestString);
            if(StringUtils.isBlank(responseString)){
                return BaseResponseEnum.ERROR;
            }
            JSONObject fateResponse = JSONObject.parseObject(responseString);
            if (FateResCode.SUCCESS.equals(fateResponse.getString("retcode"))) {
                return BaseResponseEnum.SUCCESS;
            } else {
                log.error("fateResCode is not 0, res is {}", responseString);
                return BaseResponseEnum.ERROR;
            }
        } catch (Exception e) {
            log.error("get response error : ", e);
            return BaseResponseEnum.ERROR;
        }
    }

    /**
     * 请求Fate平台提交任务接口
     * @param requestString 请求参数
     * @return BaseResponseEnum
     */
    public static BaseResponse jobSubmit(String requestString) {

        String url = getUrl() +"/job/submit";
        String responseString;
        try {
            responseString = OKHttpUtils.getPostResponse(url, requestString);
            if(StringUtils.isBlank(responseString)){
                return BaseResponse.error();
            }
            JSONObject fateResponse = JSONObject.parseObject(responseString);
            if (FateResCode.SUCCESS.equals(fateResponse.getString("retcode"))) {
                return BaseResponse.success(fateResponse);
            } else {
                log.error("fateResCode is not 0, res is {}", responseString);
                return BaseResponse.error();
            }
        } catch (Exception e) {
            log.error("get response error : ", e);
            return BaseResponse.error();
        }
    }

    /**
     * 请求Fate平台查询任务状态接口
     * @param requestString 请求参数
     * @return BaseResponseEnum
     */
    public static BaseResponse jobQuery(String requestString) {

        String url = getUrl() +"/job/query";
        String responseString;
        try {
            responseString = OKHttpUtils.getPostResponse(url, requestString);
            if(StringUtils.isBlank(responseString)){
                return BaseResponse.error();
            }
            JSONObject fateResponse = JSONObject.parseObject(responseString);
            if (FateResCode.SUCCESS.equals(fateResponse.getString("retcode"))) {
                return BaseResponse.success(fateResponse);
            } else {
                log.error("fateResCode is not 0, res is {}", responseString);
                return BaseResponse.error();
            }
        } catch (Exception e) {
            log.error("get response error : ", e);
            return BaseResponse.error();
        }
    }

    /**
     * 请求Fate平台获取数据
     * @param requestString 请求参数
     * @return BaseResponseEnum
     */
    public static BaseResponse outputData(String requestString) {

        String url = getUrl() +"/tracking/component/output/data";
        String responseString;
        try {
            responseString = OKHttpUtils.getPostResponse(url, requestString);
            if(StringUtils.isBlank(responseString)){
                return BaseResponse.error();
            }
            JSONObject fateResponse = JSONObject.parseObject(responseString);
            if (FateResCode.SUCCESS.equals(fateResponse.getString("retcode"))) {
                return BaseResponse.success(fateResponse);
            } else {
                log.error("fateResCode is not 0, res is {}", responseString);
                return BaseResponse.error();
            }
        } catch (Exception e) {
            log.error("get response error : ", e);
            return BaseResponse.error();
        }
    }

    /**
     * 请求Fate平台下载数据
     * @param requestString 请求参数
     * @return BaseResponseEnum
     */
    public static BaseResponse downloadData(String requestString,String tarFilePath,String outputDir,String csvFilePath) {

        String url = getUrl() +"/tracking/component/output/data/download";

        try {
            boolean response = ApacheHttpUitls.getStreamGetResponse(url, requestString, tarFilePath);
            if(response){
                // 解压文件
                boolean res = CompressUtil.decompress(tarFilePath,outputDir,csvFilePath);
                if (res){
                    return BaseResponse.success();
                }else{
                    return BaseResponse.error();
                }
            } else {
                return BaseResponse.error();
            }

        } catch (Exception e) {
            log.error("get response error : ", e);
            return BaseResponse.error();
        }
    }

}
